package com.yuwenchao.superafflatus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuwenchao.superafflatus.pojo.entity.SuperUrlFavorite;
import com.yuwenchao.superafflatus.service.SuperUrlFavoriteService;
import com.yuwenchao.superafflatus.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChaooGG
 * @since 2022-01-27
 */
@RestController
@RequestMapping("/superUrlFavorite")
@Api(tags = "收藏网址测试")
public class SuperUrlFavoriteController {

    @Autowired
    SuperUrlFavoriteService superUrlFavoriteService;


    @ApiOperation("查询Url列表")
    @GetMapping("getUrlList")
    public Map getUrlList(@RequestParam(value="page",defaultValue="1") Integer page,
                          @RequestParam(value="limit",defaultValue="10") Integer limit){
       // List<SuperUrlFavorite> list = superUrlFavoriteService.list();
        Map<String,Object> map = new HashMap<>();
        Page<SuperUrlFavorite> pageParam = new Page<>(page,limit);
        Page<SuperUrlFavorite> infoPage = superUrlFavoriteService.page(pageParam,null);
        map.put("count", infoPage.getTotal());
        map.put("data", infoPage.getRecords());
        map.put("code", 0);
        map.put("msg", "");

        return map;
    }

    @ApiOperation("新增网址")
    @PostMapping("saveSuperUrlFavorite")
    public  R saveSuperUrlFavorite(@RequestBody SuperUrlFavorite superUrlFavorite){
        superUrlFavoriteService.save(superUrlFavorite);
        return R.ok();
    }


    /**
     * 单个删除
     */
    @ApiOperation("根据id删除网址")
    @DeleteMapping("delUrlFavorite/{id}")
    public R delUrlFavorite(@PathVariable Integer id) {
        //superTableMapper.deleteById(id);
        superUrlFavoriteService.removeById(id);
        return R.ok();
    }


    @ApiOperation("根据id修改网址")
    @PostMapping("editUrlFavoriteById/{id}")
    public R editInfoById(@PathVariable Integer id,@RequestBody SuperUrlFavorite superUrlFavorite) {
        superUrlFavoriteService.updateById(superUrlFavorite);
        return R.ok();
    }



    @ApiOperation("批量删除网址")
    @DeleteMapping("delAllUrlFavorite")
    public R delAllUrlFavorite(@RequestParam("ids") String ids) {
        //System.out.println("获取到的ids"+ids);
        if(ids.contains(",")) {
            List<Integer> list = new ArrayList<>();
            String[] split =ids.trim().split(",");
            for (String string : split) {
                list.add(Integer.parseInt(string));
            }
            System.out.println(list);
            //superTableMapper.deleteBatchIds(list);
            superUrlFavoriteService.removeByIds(list);
        }else {
            Integer id = Integer.parseInt(ids);
            //superTableMapper.deleteById(id);
            superUrlFavoriteService.removeById(id);
        }
        return R.ok();
    }

    @ApiOperation("根据名称删除网址")
    @DeleteMapping("delSuperUrlFavorite/{superName}")
    public  R delSuperUrlFavoriteByName(@PathVariable String superName){
        QueryWrapper<SuperUrlFavorite> superUrlFavoriteQueryWrapper = new QueryWrapper<>();
        superUrlFavoriteQueryWrapper.eq("super_name",superName);
        boolean remove = superUrlFavoriteService.remove(superUrlFavoriteQueryWrapper);
        if(remove){
            return R.ok();
        }else{
            return R.error().message("网址名称不存在!");
        }

    }


    @ApiOperation("网址搜索查询")
    @PostMapping("/searchUrlByName")
    public Map searchUrlByName(@RequestParam(value="page",defaultValue="1") Integer page,
                                @RequestParam(value="limit",defaultValue="10") Integer limit,
                                @RequestParam(value="superName",required = false) String superName,
                                HttpSession session){

        Page<SuperUrlFavorite> pageParam = new Page<>(page,limit);
        QueryWrapper<SuperUrlFavorite> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.like(StringUtils.isNotBlank(superName),"super_name",superName);

        if(StringUtils.isNotBlank(superName)){
            session.setAttribute("name",superName);
        }

        Page<SuperUrlFavorite> infoPage = superUrlFavoriteService.page(pageParam,infoQueryWrapper);
        //        //返回值对象信息
        HashMap<String,Object> map = new HashMap<>();
        map.put("count", infoPage.getTotal());
        map.put("data", infoPage.getRecords());
        map.put("code", 0);
        map.put("msg", "");
        //
        return map;
    }




}

